__all__ = ["bookanjournal_bookanjournalinit_callback",
           "bookanjournal_bookanjournalhome_callback",
           "bookanjournal_bookanjournallist_callback",
           "bookanjournal_bookanjournalyear_callback",
           "bookanjournal_bookanjournalissue_callback",
           "bookanjournal_bookanjournalimage_callback",
           "bookanjournal_bookanjournalimage_etl_callback",
           "bookanjournal_bookanjournalsearch_callback"

           ]

import base64
import copy
import json
import math

from apps.crawler_platform.core_platform.core_g import CoreSqlValue
from apps.crawler_platform.core_platform.g_model import CallBackModel, JournalHomeModel, DealModel, DealInsertModel, \
    JournalListModel, JournalVolumeModel, JournalIssueModel, EtlCallBackModel, JournalEtlArticleModel, HtmlModel, \
    EtlDealModel


def bookanjournal_bookanjournalinit_callback(callmodel: CallBackModel[JournalHomeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts

    # print(para_dicts)
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}

    di_model_next = DealInsertModel()
    di_model_next.insert_pre = CoreSqlValue.replace_it

    if "1_1" in para_dicts["data"]:
        lists = para_dicts["data"]["1_1"]["data"]
        for i in lists:
            if i["sublevels"] == []:
                dicts = copy.deepcopy(i)
                del dicts["sublevels"]
                dicts["is_recommend"] = "true"

                temp_info = info_dicts.copy()
                task_tag_next = temp_info["task_tag_next"]
                temp_info["task_tag"] = task_tag_next
                del temp_info["task_tag_next"]
                temp_info["home_rawid"] = dicts["id"]
                temp_info["sub_db_id"] = ""
                temp_info["is_active"] = 1
                temp_info["page_index"] = 1
                temp_info["home_json"] = json.dumps(dicts, ensure_ascii=False)
                di_model_next.lists.append(temp_info)
            else:
                dicts = copy.deepcopy(i)
                del dicts["sublevels"]
                dicts["is_recommend"] = "false"
                for item in i["sublevels"]:
                    dicts["sub_class"] = copy.deepcopy(item)

                    temp_info = info_dicts.copy()
                    task_tag_next = temp_info["task_tag_next"]
                    temp_info["task_tag"] = task_tag_next
                    del temp_info["task_tag_next"]
                    temp_info["home_rawid"] = dicts["sub_class"]["id"]
                    temp_info["sub_db_id"] = ""
                    temp_info["is_active"] = 1
                    temp_info["page_index"] = 1
                    temp_info["home_json"] = json.dumps(dicts, ensure_ascii=False)
                    di_model_next.lists.append(temp_info)
    else:
        raise Exception("下载错误")
    result.next_dicts.insert.append(di_model_next)

    return result


def bookanjournal_bookanjournalhome_callback(callmodel: CallBackModel[JournalHomeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts

    # print(para_dicts)
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}

    di_model_next = DealInsertModel()
    di_model_next.insert_pre = CoreSqlValue.insert_ig_it
    if "1_1" in para_dicts["data"]:
        sql_dict = callmodel.sql_model.dict()
        if str(sql_dict["page_index"]) == str(1):
            dicts = para_dicts["data"]["1_1"]["data"]
            totalNum = dicts["totalNum"]
            total_page = math.ceil(int(totalNum) / 50)
            # result.code_dicts = {
            #     "1_2": {"max_page": total_page}
            # }
            result.befor_dicts.update.update({'page': total_page, "is_active": 1})
            di_model_bef = DealInsertModel()
            di_model_bef.insert_pre = CoreSqlValue.insert_ig_it
            for i in range(2, total_page + 1):
                sql_dict = callmodel.sql_model.dict()
                sql_dict.pop("id")
                sql_dict.pop("update_time")
                sql_dict.pop("create_time")
                sql_dict.pop("null_dicts")
                sql_dict.pop("err_msg")
                sql_dict.pop("other_dicts")
                sql_dict.pop("state")
                sql_dict.pop("failcount")
                sql_dict["page"] = total_page
                sql_dict["page_index"] = i
                sql_dict["is_active"] = 1
                di_model_bef.lists.append(sql_dict.copy())
            result.befor_dicts.insert.append(di_model_bef)

    if "1_2" in para_dicts["data"]:
        lists = para_dicts["data"]["1_2"]["data"]
        for item in lists:
            dicts = copy.deepcopy(item)
            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["journal_rawid"] = dicts["resourceId"]
            temp_info["sub_db_id"] = ""
            temp_info["is_active"] = 1
            temp_info["journal_json"] = json.dumps(dicts, ensure_ascii=False)
            di_model_next.lists.append(temp_info)
    result.next_dicts.insert.append(di_model_next)

    return result


def bookanjournal_bookanjournalsearch_callback(callmodel: CallBackModel[JournalHomeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts

    # print(para_dicts)
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}

    di_model_next = DealInsertModel()
    di_model_next.insert_pre = CoreSqlValue.insert_ig_it
    if "1_1" in para_dicts["data"]:
        lists = para_dicts["data"]["1_1"]["data"]
        totalNum = para_dicts["data"]["1_1"]["totalNum"]
        total_page = math.ceil(int(totalNum) / 50)
        # result.code_dicts = {
        #     "1_2": {"max_page": total_page}
        # }
        result.befor_dicts.update.update({'page': total_page, "is_active": 1})
        di_model_bef = DealInsertModel()
        di_model_bef.insert_pre = CoreSqlValue.insert_ig_it
        for i in range(2, total_page + 1):
            sql_dict = callmodel.sql_model.dict()
            sql_dict.pop("id")
            sql_dict.pop("update_time")
            sql_dict.pop("create_time")
            sql_dict.pop("null_dicts")
            sql_dict.pop("err_msg")
            sql_dict.pop("other_dicts")
            sql_dict.pop("state")
            sql_dict.pop("failcount")
            sql_dict["page"] = total_page
            sql_dict["page_index"] = i
            sql_dict["is_active"] = 1
            di_model_bef.lists.append(sql_dict.copy())
        result.befor_dicts.insert.append(di_model_bef)

        for item in lists:
            dicts = copy.deepcopy(item)
            dicts["get_type"] = "search"
            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["journal_rawid"] = dicts["resourceId"]
            temp_info["sub_db_id"] = ""
            temp_info["is_active"] = 1
            temp_info["journal_json"] = json.dumps(dicts, ensure_ascii=False)
            # print(temp_info["journal_json"])
            di_model_next.lists.append(temp_info)
    result.next_dicts.insert.append(di_model_next)

    return result


def bookanjournal_bookanjournallist_callback(callmodel: CallBackModel[JournalListModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts

    # print(para_dicts)
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "journal_rawid": callmodel.sql_model.journal_rawid,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}
    di_model_next = DealInsertModel()
    di_model_next.insert_pre = CoreSqlValue.insert_ig_it
    if "1_1" in para_dicts["data"]:
        lists = para_dicts["data"]["1_1"]["data"]
        for item in lists:
            dicts = copy.deepcopy(item)
            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["sub_db_id"] = ""
            temp_info["pub_year"] = item["year"]
            temp_info["num"] = ""
            temp_info["volume_json"] = json.dumps(dicts, ensure_ascii=False)
            di_model_next.lists.append(temp_info)

    result.next_dicts.insert.append(di_model_next)

    return result


def bookanjournal_bookanjournalyear_callback(callmodel: CallBackModel[JournalVolumeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts

    # print(para_dicts)
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "journal_rawid": callmodel.sql_model.journal_rawid,
                  "pub_year": callmodel.sql_model.pub_year,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}
    di_model_next = DealInsertModel()
    di_model_next.insert_pre = CoreSqlValue.insert_ig_it
    if "1_1" in para_dicts["data"]:
        lists = para_dicts["data"]["1_1"]["data"]
        for item in lists:
            dicts = copy.deepcopy(item)
            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["sub_db_id"] = ""
            temp_info["num"] = item["issueNo"]
            temp_info["issue_json"] = json.dumps(dicts, ensure_ascii=False)
            di_model_next.lists.append(temp_info)

    result.next_dicts.insert.append(di_model_next)

    return result


def bookanjournal_bookanjournalissue_callback(callmodel: CallBackModel[JournalIssueModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts

    # print(para_dicts)
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}
    # print(info_dicts)
    lists_one = para_dicts["data"]["1_1"]["data"]
    count = lists_one[0]["count"]

    dicts = json.loads(callmodel.sql_model.issue_json)
    dicts["next_info"] = copy.deepcopy(lists_one[0])
    dicts["count"] = count

    result.befor_dicts.update = {"issue_json": json.dumps(dicts, ensure_ascii=False)}

    di_model_next = DealInsertModel()
    di_model_next.insert_pre = CoreSqlValue.insert_ig_it

    if "1_3" in para_dicts["data"]:
        task_info = callmodel.redis_all.parse_dict["1_3"].task_info
        info_dicts = {"task_name": callmodel.sql_model.task_name,
                      "task_tag": callmodel.sql_model.task_tag,
                      "sub_db_id": callmodel.sql_model.sub_db_id,
                      "task_tag_next": task_info.task_tag_next}
        lists = para_dicts["data"]["1_3"]["data"]
        for item in lists:
            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["sub_db_id"] = ""
            rawid = ",".join([callmodel.sql_model.journal_rawid, callmodel.sql_model.pub_year, callmodel.sql_model.num,
                              str(item["page"]), str(item["hash"])])
            temp_info["rawid"] = rawid

            item["journal_rawid"] = callmodel.sql_model.journal_rawid
            item["pub_year"] = callmodel.sql_model.pub_year
            item["num"] = callmodel.sql_model.num
            issue_json_dicts = json.loads(callmodel.sql_model.issue_json)
            item["issueId"] = issue_json_dicts["issueId"]
            item["webp"] = lists_one[0]["webp"]

            temp_info["article_info_json"] = json.dumps(copy.deepcopy(item), ensure_ascii=False)
            di_model_next.lists.append(temp_info)

        result.next_dicts.insert.append(di_model_next)

    else:
        if "1_2" in para_dicts["data"]:
            lists = para_dicts["data"]["1_2"]["data"]
            for item in lists:
                if item["id"] == 0:
                    dicts = copy.deepcopy(item)
                    del dicts["sublevels"]
                    for item_one in item["sublevels"]:
                        temp_info = info_dicts.copy()
                        task_tag_next = temp_info["task_tag_next"]
                        temp_info["task_tag"] = task_tag_next
                        del temp_info["task_tag_next"]
                        temp_info["sub_db_id"] = ""
                        temp_info["rawid"] = str(item_one["id"])

                        item_one["super"] = copy.deepcopy(dicts)
                        item_one["count"] = count
                        item_one["journal_rawid"] = callmodel.sql_model.journal_rawid
                        item_one["pub_year"] = callmodel.sql_model.pub_year
                        item_one["num"] = callmodel.sql_model.num

                        temp_info["article_info_json"] = json.dumps(copy.deepcopy(item_one), ensure_ascii=False)
                        di_model_next.lists.append(temp_info)

        result.next_dicts.insert.append(di_model_next)

        # print(result)
    return result


def bookanjournal_bookanjournalimage_callback(callmodel: CallBackModel[JournalIssueModel]) -> DealModel:
    result = DealModel()
    return result


def bookanjournal_bookanjournalimage_etl_callback(
        callmodel: EtlCallBackModel[JournalEtlArticleModel, HtmlModel]) -> EtlDealModel:
    result = EtlDealModel()
    para_dicts = callmodel.para_dicts
    # print(para_dicts)

    img_str = para_dicts["data"]["1_1"]["html"]
    new_img_str = json.loads(img_str)
    img_str = new_img_str["image_base64"]
    bt = img_str.encode("utf-8")
    img_data = base64.b64decode(
        bt)

    rawid = callmodel.sql_model.rawid
    journal_rawid, pub_year, num, page, hashcode = rawid.split(",")
    path = "/".join(["pic", "bookanpdfimage", journal_rawid, pub_year, num])
    minio_path = path + "/" + page + ".jpg"
    # minio_bk = "crawl.dc.cqvip.com"
    # print(minio_path)
    result.other_info = {"image": {"path": minio_path, "image_data": img_data}}
    return result
